package br.com.ds3.controller;

import java.util.List;

import org.hibernate.criterion.Order;

import br.com.caelum.vraptor.Get;
import br.com.caelum.vraptor.Resource;
import br.com.caelum.vraptor.Result;
import br.com.ds3.annotation.Permission;
import br.com.ds3.dao.InscricaoDAO;
import br.com.ds3.dao.UsuarioDAO;
import br.com.ds3.model.Inscricao;
import br.com.ds3.model.Perfil;
import br.com.ds3.session.UserSession;

@Resource
public class ParticipanteController {
	private final Result result;
	private final UserSession userSession;
	private final InscricaoDAO inscricaoDAO;

	public ParticipanteController(Result result, UserSession userSession, InscricaoDAO inscricaoDAO) {
		this.result = result;
		this.userSession = userSession;
		this.inscricaoDAO = inscricaoDAO;
	}
	
	@Get
	@Permission(value = { Perfil.PARTICIPANTE, Perfil.ATENDENTE, Perfil.ADMINISTRADOR })
	public void meusDados(String evento, Long pagina) {
		pagina = pagina == null ? new Long(1) : pagina;
		List<Inscricao> inscricoes = inscricaoDAO.pesquisa(userSession.getUser(), null, false, pagina, Order.desc("a.data"));
		Long paginas = inscricaoDAO.countPaginas(userSession.getUser(), null, false);
		result.include("inscricoes", inscricoes);
		result.include("pagina", pagina);
		result.include("paginas", paginas);
	}
	
	@Get
	@Permission(value = { Perfil.PARTICIPANTE, Perfil.ATENDENTE, Perfil.ADMINISTRADOR })
	public void alterarDados() {
		result.forwardTo(UsuarioController.class).cadastro(new UsuarioDAO().get(userSession.getUser().getId()));
	}
}
